// order_pkg/pages/settlement/settlement.js
Page({
  data: {
    cartIds: '',
    mode: '',
    couponId: '',
    checkOrder: {},
    balance: '',
    containerShow: false
  },
  onLoad(query) {
    const cartIds = query.cartIds
    const mode = query.mode
    this.setData({cartIds, mode})
    this.getCheckOrder()
  },
  onShow() {
    this.onRefreshPage()
  },
  async onRefreshPage() {
    this.getCheckOrder()
    this.getUserBalance()
  },
  // 获取订单结算信息
  async getCheckOrder() {
    const res = await wx.http.get('/checkout/order', {
      mode: this.data.mode,
      cartIds: this.data.cartIds,
      delivery: '',
      couponId: this.data.couponId,
      isUsePoints: 0
    })
    const checkOrder = res.data.data.order
    this.setData({checkOrder})
  },
  // 获取用户余额
  async getUserBalance() {
    const res = await wx.http.get('/user/assets')
    const balance = res.data.data.assets.balance
    this.setData({balance})
  },
  // 更改地址
  toAddress() {
    wx.navigateTo({
      url: '../../../user_pkg/pages/address/address',
    })
  },
  // 使用优惠券
  openContainer() {
    this.setData({containerShow: true})
  },
  closeContainer() {
    this.setData({couponId: ''})
    this.setData({containerShow: false})
    this.getCheckOrder()
  },
  async useCoupon(ev) {
    const couponId = ev.mark.couponId
    await this.setData({couponId})
    this.getCheckOrder()
    this.setData({containerShow: false})
  },
  // 提交订单
  async submitOrder() {
    const res = await wx.http.post('/checkout/submit', {
      mode: this.data.mode,
      cartIds: this.data.cartIds,
      delivery: '10',
      couponId: this.data.couponId,
      isUsePoints: 0,
      payType: '10',
      remark: ''
    })
    wx.redirectTo({
      url: '../order/order',
    })
  }
})